package com.atguigu.rabbitmq.springbootmq.contorller;

import com.atguigu.rabbitmq.springbootmq.config.MyCallBack;
import lombok.extern.slf4j.Slf4j;
import org.mapstruct.Qualifier;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.CorrelationDataPostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;

/**
 * @description: TODO 使用发布确认机制 保证 消息发布到 交换机中
 * @author zhangbanglong
 * @date 2024/1/14 14:30
 * @version 1.0
 */
@RestController
@RequestMapping("/confirm")
@Slf4j
public class Producer {

    public static final String EXCHANGE_NM="confirm.exchange";

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private MyCallBack myCallBack;

    @PostConstruct
    public void init(){
        rabbitTemplate.setConfirmCallback(myCallBack);
    }

    @GetMapping("sendMessage/{message}")
    public void sendMessage(@PathVariable String message){
        CorrelationData data=new CorrelationData("11");
        String routingKey="key1";
        rabbitTemplate.convertAndSend(EXCHANGE_NM,routingKey,message+routingKey,data);

        CorrelationData data2=new CorrelationData("22");
         routingKey="key2";
        rabbitTemplate.convertAndSend(EXCHANGE_NM,routingKey,message+routingKey,data2);
    }
}
